package com.gitee.sop.sopauth.controller;

import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.servercommon.bean.OpenContext;
import com.gitee.sop.servercommon.bean.ServiceContext;
import com.gitee.sop.servercommon.exception.ServiceException;
import com.gitee.sop.servercommon.result.R;
import com.gitee.sop.servercommon.util.StringUtils;
import com.gitee.sop.sopauth.auth.*;
import com.gitee.sop.sopauth.service.SendCodeHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 授权认证
 *
 * @author admin
 */
@Api(tags = "认证接口")
@RestController
@RequestMapping("oauth2")
public class OAuth2Controller {

    @Autowired
    private OAuth2Service oAuth2Service;

    @Autowired
    private SendCodeHandler sendCodeHandler;

    @ApiOperation(value = "登录/注册", notes = "登录/注册")
    @Open(value = "open.auth.token.authorize")
    @PostMapping("authorize")
    public Object authorize(@RequestBody FetchTokenParam authParam) {
        OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
        authParam.setAppId(openContext.getAppId());
        sendCodeHandler.validateCode(authParam);
        OAuth2Config instance = OAuth2Config.getInstance();
        FetchTokenResult tokenResult = oAuth2Service.authorize(authParam, instance);
        if (StringUtils.isNull(tokenResult)) {
            throw new ServiceException("认证失败");
        }
        return R.ok(tokenResult);
    }

    @ApiOperation(value = "登录", notes = "登录")
    @Open(value = "open.auth.token.login")
    @PostMapping("login")
    public Object login(@RequestBody FetchTokenParam authParam) {
        OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
        authParam.setAppId(openContext.getAppId());
        OAuth2Config instance = OAuth2Config.getInstance();
        FetchTokenResult tokenResult = oAuth2Service.simpleLogin(authParam, instance);
        if (StringUtils.isNull(tokenResult)) {
            throw new ServiceException("登录失败");
        }
        return R.ok(tokenResult);
    }

    @ApiOperation(value = "注册", notes = "注册")
    @Open(value = "open.auth.token.register")
    @PostMapping("register")
    public Object register(@RequestBody FetchTokenParam authParam) {
        OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
        authParam.setAppId(openContext.getAppId());
        OAuth2Config instance = OAuth2Config.getInstance();
        FetchTokenResult tokenResult = oAuth2Service.simpleRegister(authParam, instance);
        if (StringUtils.isNull(tokenResult)) {
            throw new ServiceException("登录失败");
        }
        return R.ok(tokenResult);
    }

    @ApiOperation(value = "重置密码", notes = "重置密码")
    @Open(value = "open.auth.token.updatePwd", needToken = true)
    @PutMapping("updatePwd")
    public Object updatePwd() {
        //
        return R.ok();
    }

    @ApiOperation(value = "获取登录用户信息", notes = "获取登录用户信息")
    @Open(value = "open.auth.token.info", needToken = true)
    @GetMapping("getInfo")
    public Object getInfo() {
        OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
        String appAuthToken = openContext.getAppAuthToken();
        OpenUser info = oAuth2Service.getInfo(appAuthToken);
        return R.ok(info);
    }
}
